﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections;
using UnityEngine;

namespace Assets.Scripts.DL.Player.States.Root.Special {

	/// <summary>
	/// 弹反
	/// </summary>
	public class PlayerRebound : PlayerBaseState {
		bool _isReboundOpen;
		PlayerStateEnum[] _movement;
		int _movementIndex = 0;
		String[] _swdAudio = new string[] { AudioStr.PlayerSwdAtk1Audio, AudioStr.PlayerSwdAtk2Audio };
		public PlayerRebound(PlayerController controller, PlayerStateEnum stateName, State<PlayerStateEnum> superFSM, StateFactory<PlayerStateEnum> factory) : base(controller, stateName, superFSM, factory) {
			AddConditionToSelf(() => _controller.ReboundInput);
			_movement = new PlayerStateEnum[] { PlayerStateEnum.GroundSwd1, PlayerStateEnum.GroundSwd2 };
		}

		public override void Enter() {
			//_controller.PlayAnim(PlayerStateEnum.Rebound);
			_controller.Velocity = Vector2.zero;
			_controller.PlayAnim(_movement[(_movementIndex++) % 2]);//动作一样的
			AudioManager.Play(_controller.Effect, _swdAudio[(_movementIndex++) % 2]);
			_isReboundOpen = false;
		}
		public override void Update() {
			base.Update();
			//此处的代码主要作用于弹反远程
			//近战很不优雅的卸载了TakeDamage中
			//if (_timer.Elapsed > Time.deltaTime) {
			if (_controller.IsAnimPlayAt(_controller.ConfigData.ReboundActiveRange.x) && !_isReboundOpen) {
				_isReboundOpen = true;
				//Debug.Log("open");
				_controller.SetReboundCheckColl(true);
			}
			//Debug.Log("CheckFrame");
			if (_isReboundOpen && _controller.IsAnimPlayAt(_controller.ConfigData.ReboundActiveRange.y)) {
				//Debug.Log("close");
				_controller.SetReboundCheckColl(false);
			}
			if (_controller.IsAnimPlayOver())
				_controller.StateData.IsSpecial = false;
			//}
		}

		public override void Exit() {
		}

	}
}
